package com.kuaixueit.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaixueit.crm.mapper.CPositionMapper;
import com.kuaixueit.crm.pojo.CPosition;
import com.kuaixueit.crm.pojo.ResultVO;
import com.kuaixueit.crm.service.PositionService;

@Service
public class PositionServiceImpl implements PositionService {

	
	@Autowired
	private CPositionMapper positionMapper;
	
	
	@Override
	public void savePosition(CPosition position) {
		Date date = new Date();
		position.setCreateTime(date);
		position.setUpdateTime(date);
		positionMapper.insert(position);
	}


	@Override
	public PageInfo findPage(Integer deptid,String positionName,Integer page, Integer rows) {
		//分页插件的使用
		PageHelper.startPage(page, rows);
		//查询
		List<CPosition> list = new ArrayList<CPosition> ();
		//查询数据库
		list = positionMapper.findList(deptid,"%"+positionName+"%");
		//查询数据交给分页插件，自动查询总数
		PageInfo info = new PageInfo(list);
		System.out.println("总数："+info.getTotal()+" 当前页数据："+info.getList());
		return info;
	}


	@Override
	public void deleteByIds(Integer[] ids) {
		
		for (int i = 0; i < ids.length; i++){
			//假如ids有10个，删除到第六个出错了
			//2，事务回滚，都没有删除成功
			int deleteByPrimaryKey = positionMapper.deleteByPrimaryKey(ids[i]);
		}
		
	}


	@Override
	public ResultVO update(CPosition position) {
		Integer id = position.getId();
		if(null != id){
			positionMapper.updateByPrimaryKey(position);
			return new ResultVO("修改成功");
		}
		return new ResultVO("修改失败");
	}

}
